# -*- coding: utf-8 -*-

import httpx
from bs4 import BeautifulSoup
from datetime import datetime
import re
import asyncio
import  asyncio
from playwright.async_api import async_playwright
from urllib.parse import urlparse

header = {
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/604.1 Edg/112.0.100.0'}


# 只爬https://movie.douban.com开头的网页
async def mao_yan_crawler(url: str, logger) -> (int, dict):
    if not url.startswith('https://www.maoyan.com') and not url.startswith('http://www.maoyan.com'):
        logger.warning(f'{url} is not a maoyan url, you should not use this function')
        return -5, {}

    url = url.replace("http://", "https://", 1)

    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=False)
        page = await browser.new_page()
        await page.goto(url)

        # 设置 div 的 data-val 属性
        # await page.evaluate("""
        #         const div = document.querySelector('div.city-container');
        #         if (div) {
        #             div.setAttribute('data-val', JSON.stringify({currentcityid: 1}));
        #         }
        #     """
        # )
        #
        # # 重新加载页面
        # await page.reload()
        await page.get_by_label('').select_option(label='北京')

        divs = await page.locator('div.city-name').all()
        divvv = await divs[0].inner_html()

        # 查找城市选择下拉框并选择北京市
        city_selector = page.locator('//*div[contains(@class, "city-name")]')
        await city_selector.select_option(label='北京')

        # 等待页面刷新或其它操作
        await page.wait_for_timeout(2000)

        # parsed_url = urlparse(url)
        # path = parsed_url.path


